package com.yb0os1.serve.impl;

import com.yb0os1.entity.Orders;
import com.yb0os1.entity.TurnoverStatistics;
import com.yb0os1.entity.UserStatistics;
import com.yb0os1.mapper.DishMapper;
import com.yb0os1.mapper.OrderMapper;
import com.yb0os1.mapper.SetmealMapper;
import com.yb0os1.mapper.UserMapper;
import com.yb0os1.serve.WorkspaceServe;
import com.yb0os1.vo.BusinessDataVO;
import com.yb0os1.vo.DishOverViewVO;
import com.yb0os1.vo.OrderOverViewVO;
import com.yb0os1.vo.SetmealOverViewVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.math.RoundingMode;
import java.time.LocalDate;
import java.util.List;

@Service
public class WorkspaceServeImpl implements WorkspaceServe {
    @Override
    public OrderOverViewVO overviewOrders() {
        LocalDate today = LocalDate.now();
        Integer completedOrders = orderMapper.getOrdersStatistics(today, Orders.COMPLETED, true);
        Integer waitingOrders = orderMapper.getOrdersStatistics(today, Orders.CONFIRMED, true);
        Integer deliveredOrders = orderMapper.getOrdersStatistics(today, Orders.DELIVERY_IN_PROGRESS, true);
        Integer cancelledOrders = orderMapper.getOrdersStatistics(today, Orders.CANCELLED, true);
        Integer allOrders = orderMapper.getOrdersStatistics(today, null, true);
        return OrderOverViewVO.builder()
                .deliveredOrders(deliveredOrders)
                .cancelledOrders(cancelledOrders)
                .completedOrders(completedOrders)
                .allOrders(allOrders)
                .waitingOrders(waitingOrders)
                .build();
    }

    @Autowired
    private DishMapper dishMapper;
    @Override
    public DishOverViewVO overviewDishes() {
        Integer openSetmeal = dishMapper.selectDishSumByStatus(1);
        Integer closeSetmeal = dishMapper.selectDishSumByStatus(0);

        return DishOverViewVO.builder()
                .sold(openSetmeal)
                .discontinued(closeSetmeal)
                .build();
    }

    @Override
    public SetmealOverViewVO overviewSetmeals() {
        Integer openSetmeal = setmealMapper.selectSetmealStatus(1);
        Integer closeSetmeal = setmealMapper.selectSetmealStatus(0);

        return SetmealOverViewVO.builder()
                .sold(openSetmeal)
                .discontinued(closeSetmeal)
                .build();
    }

    @Autowired
    private UserMapper userMapper;

    @Autowired
    private SetmealMapper setmealMapper;

    @Autowired
    private OrderMapper orderMapper;
    @Override
    public BusinessDataVO getBusinessDataVO() {
        LocalDate today = LocalDate.now();
        Integer userNumber = 0;
        BigDecimal turnover  = BigDecimal.valueOf(0);
        List<UserStatistics> userStatistics = userMapper.getUserStatistics(today, today);
        if (userStatistics!=null&&!userStatistics.isEmpty())
            userNumber = userStatistics.get(0).getNewUserNumber();
        Integer orderNumber = orderMapper.getOrdersStatistics(today, null, true);
        Integer validOrderNumber = orderMapper.getOrdersStatistics(today, Orders.COMPLETED, true);
        BigDecimal orderCompletionRate = orderNumber==0?BigDecimal.valueOf(0):BigDecimal.valueOf(validOrderNumber).divide(BigDecimal.valueOf(orderNumber),4, RoundingMode.HALF_UP);
        List<TurnoverStatistics> turnoverStatistics = orderMapper.getTurnoverStatistics(today, today);
        if (turnoverStatistics!=null&&!turnoverStatistics.isEmpty())
            turnover=turnoverStatistics.get(0).getMoney();
        BigDecimal unitPrice = orderNumber==0?BigDecimal.valueOf(0):turnover.divide(BigDecimal.valueOf(orderNumber),4, RoundingMode.HALF_UP);
        return BusinessDataVO.builder()
                .unitPrice(unitPrice)
                .turnover(turnover)
                .newUsers(userNumber)
                .orderCompletionRate(orderCompletionRate)
                .validOrderCount(validOrderNumber)
                .build();
    }
}
